package dataview;

import org.jdom.Element;
import store.Termine;

public class PerTermine extends CriterioRicerca {
	private Termine termine;
	private boolean eun;
	
	public PerTermine(Ordinamento ordine, Termine termine, boolean eun) {
		super.ordinamento = ordine;
		this.termine = termine;
		this.eun = eun;
	}
	
	public Ordinamento getOrdinamento() {
		return ordinamento;
	}
	
	public Termine getTermine() {
		return termine;
	}
	
	public boolean getEun() {
		return eun;
	}
	
	public static PerTermine fromXml(Element elemento) {
		if (elemento.getName() != "per_termine") throw new IllegalArgumentException("L'elemento passato come parametro non rappresenta una ricerca per termine");
		Element e = elemento.getChild("termine");
		Termine t = new Termine(TipoTermine.valueOf(e.getChildText("tipo")), e.getChildText("termine"));
		return new PerTermine(Ordinamento.valueOf(elemento.getChildText("ordinamento")), t, Boolean.parseBoolean(elemento.getChildText("appare")));
	}
	
	@Override
	public Element toXml() {
		Element e = new Element("per_termine");
		Element t = new Element("termine");
		t.addContent(new Element("tipo").setText(termine.getTipo().toString()));
		t.addContent(new Element("termine").setText(termine.getTermine()));
		e.addContent(t);
		e.addContent(new Element("appare").setText(Boolean.toString(eun)));
		e.addContent(new Element("ordinamento").setText(ordinamento.toString()));
		return e;
	}
	
	//PRIMA NON CONSIDERAVO L'ORDINE
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + (eun ? 1231 : 1237);
		result = prime * result + ((termine == null) ? 0 : termine.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (!super.equals(obj)) return false;
		if (getClass() != obj.getClass()) return false;
		PerTermine other = (PerTermine)obj;
		if (eun != other.eun) return false;
		if (termine == null) {
			if (other.termine != null) return false;
		} else if (!termine.equals(other.termine)) return false;
		return true;
	}
}
